using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using UIProcessSchemaObjects;

namespace UIProcessActivities
{
    [Designer(typeof(LoadFormTemplateDesigner), typeof(IDesigner))]
    public partial class LoadFormTemplate : SequenceActivity
	{
		public LoadFormTemplate()
		{
			InitializeComponent();
		}

        public static DependencyProperty TemplateNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register("TemplateName", typeof(string), typeof(LoadFormTemplate));

        [Description("Name of the Form Template to Load")]
        [Category("Input")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string TemplateName
        {
            get
            {
                return ((string)(base.GetValue(LoadFormTemplate.TemplateNameProperty)));
            }
            set
            {
                base.SetValue(LoadFormTemplate.TemplateNameProperty, value);
            }
        }

        public static DependencyProperty LoadedFormProperty = System.Workflow.ComponentModel.DependencyProperty.Register("LoadedForm", typeof(UIProcessSchemaObjects.Form), typeof(LoadFormTemplate));

        [Description("The form that was loaded by the activity")]
        [Category("Output")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public UIProcessSchemaObjects.Form LoadedForm
        {
            get
            {
                return ((UIProcessSchemaObjects.Form)(base.GetValue(LoadFormTemplate.LoadedFormProperty)));
            }
            set
            {
                base.SetValue(LoadFormTemplate.LoadedFormProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            this.LoadedForm = LoadFormFromTemplate(this.TemplateName);
            return ActivityExecutionStatus.Closed;
        }

        private Form LoadFormFromTemplate(string strFileName)
        {
            // Declare an object variable of the type to be deserialized.
            Form theForm = new Form();
            string strTempPath = Properties.Settings.Default.ApplicationTemplatePath; 
            strTempPath += "\\";
            strTempPath += strFileName;

            try
            {
                if (File.Exists(strTempPath))
                {
                    // Create an instance of the XmlSerializer class;
                    // specify the type of object to be deserialized.
                    XmlSerializer serializer = new XmlSerializer(theForm.GetType(), "http://formsanywhere.net/uiprocess/2009/03");
                    // To read the file, create a FileStream.
                    FileStream myFileStream = new FileStream(strTempPath, FileMode.Open);
                    // Call the Deserialize method and cast to the object type.
                    theForm = (Form)serializer.Deserialize(myFileStream);
                    // close the file
                    myFileStream.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }

            return theForm;
        }

	}
}
